﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace drcoderz.com.clocks.simple
{
    public enum NumberFormat
    {
        AsRoman,
        AsArabic
    }

    public class SimpleClock : Control
    {
        private Canvas arabicNumberCanvas;
        private Canvas romanNumberCanvas;

        private RotateTransform hourHandTransform;
        private RotateTransform minuteHandTransform;
        private RotateTransform secondHandTransform;


        public NumberFormat ClockNumberFormat
        {
            get { return (NumberFormat)GetValue(ClockNumberFormatProperty); }
            set { 
                SetValue(ClockNumberFormatProperty, value);
                if (arabicNumberCanvas != null && romanNumberCanvas != null)
                {
                    if (value == NumberFormat.AsArabic)
                    {
                        arabicNumberCanvas.Visibility = Visibility.Visible;
                        romanNumberCanvas.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        arabicNumberCanvas.Visibility = Visibility.Collapsed;
                        romanNumberCanvas.Visibility = Visibility.Visible;
                    }
                }
            }
        }

        // Using a DependencyProperty as the backing store for ClockNumberFormat.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClockNumberFormatProperty =
            DependencyProperty.Register("ClockNumberFormat", typeof(NumberFormat), typeof(SimpleClock), new PropertyMetadata(NumberFormat.AsArabic));




        public int HourValue
        {
            get { return (int)GetValue(HourValueProperty); }
            set { 
                SetValue(HourValueProperty, value);
                if (hourHandTransform != null)
                    hourHandTransform.Angle = value;
            }
        }

        // Using a DependencyProperty as the backing store for HourValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HourValueProperty =
            DependencyProperty.Register("HourValue", typeof(int), typeof(SimpleClock), new PropertyMetadata(null));



        public int MinuteValue
        {
            get { return (int)GetValue(MinuteValueProperty); }
            set { SetValue(MinuteValueProperty, value);
            if (minuteHandTransform != null)
                minuteHandTransform.Angle = value;
            }
        }

        // Using a DependencyProperty as the backing store for MinuteValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MinuteValueProperty =
            DependencyProperty.Register("MinuteValue", typeof(int), typeof(SimpleClock), new PropertyMetadata(null));




        public int SecondValue
        {
            get { return (int)GetValue(SecondValueProperty); }
            set { SetValue(SecondValueProperty, value);
            if (secondHandTransform != null)
                secondHandTransform.Angle = value;
            }
        }

        // Using a DependencyProperty as the backing store for SecondValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SecondValueProperty =
            DependencyProperty.Register("SecondValue", typeof(int), typeof(SimpleClock), new PropertyMetadata(null));



        public string ClockLabel
        {
            get { return (string)GetValue(ClockLabelProperty); }
            set { SetValue(ClockLabelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ClockLabel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClockLabelProperty =
            DependencyProperty.Register("ClockLabel", typeof(string), typeof(SimpleClock), new PropertyMetadata("QUARTZ"));




        public Brush NumberColor
        {
            get { return (Brush)GetValue(NumberColorProperty); }
            set { SetValue(NumberColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NumberColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NumberColorProperty =
            DependencyProperty.Register("NumberColor", typeof(Brush), typeof(SimpleClock), new PropertyMetadata(new SolidColorBrush(Colors.Black)));



        public Brush ClockLabelColor
        {
            get { return (Brush)GetValue(ClockLabelColorProperty); }
            set { SetValue(ClockLabelColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ClockLabelColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClockLabelColorProperty =
            DependencyProperty.Register("ClockLabelColor", typeof(Brush), typeof(SimpleClock), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

        


        public Brush HandColor
        {
            get { return (Brush)GetValue(HandColorProperty); }
            set { SetValue(HandColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HandColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HandColorProperty =
            DependencyProperty.Register("HandColor", typeof(Brush), typeof(SimpleClock), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 173, 7, 7))));

        

        public SimpleClock()
        {
            this.DefaultStyleKey = typeof(SimpleClock);
        }

        public override void OnApplyTemplate()
        {
            hourHandTransform = this.GetTemplateChild("hourHandRotation") as RotateTransform;
            minuteHandTransform = this.GetTemplateChild("minuteHandRotation") as RotateTransform;
            secondHandTransform = this.GetTemplateChild("secondHandRotation") as RotateTransform;

            hourHandTransform.Angle = HourValue;
            minuteHandTransform.Angle = MinuteValue;
            secondHandTransform.Angle = SecondValue;

            arabicNumberCanvas = this.GetTemplateChild("arabicNumbers") as Canvas;
            romanNumberCanvas = this.GetTemplateChild("romanNumbers") as Canvas;

            switch (ClockNumberFormat)
            {
                case NumberFormat.AsArabic:
                    arabicNumberCanvas.Visibility = Visibility.Visible;
                    romanNumberCanvas.Visibility = Visibility.Collapsed;
                    break;
                case NumberFormat.AsRoman:
                    arabicNumberCanvas.Visibility = Visibility.Collapsed;
                    romanNumberCanvas.Visibility = Visibility.Visible;
                    break;
            }
            
            base.OnApplyTemplate();
        }
    }
}
